#include "numberhelper.h"
#include <QtCore/qregexp.h>
#include <QtCore/qlist.h>
#include <QStack>
#include <QQueue>

QString NumberHelper::numToStr(int num)
{
    return NumberHelper::numToStr("", num, "");
}

QString NumberHelper::numToStr(QString preStr, int num, QString sufStr)
{
    if (num <= 0)
        return QString("零");
    Qt::SplitBehavior behavior(Qt::SplitBehaviorFlags::SkipEmptyParts);

    QString nums = "零,一,二,三,四,五,六,七,八,九";
    QStringList numbers = nums.split(",", behavior, Qt::CaseSensitivity::CaseSensitive);

    QString unit = "十,百,千,万,十万,百万,千万,亿,十亿,百亿,千亿,万亿";
    QStringList units = unit.split(",", behavior, Qt::CaseSensitivity::CaseSensitive);
    int len = -1;
    QStack<QString> stack;
    bool has = false;
    while (num > 0)
    {
        QString n = "";
        if (num % 10 != 0)
        {
            has = true;
        }
        if (has)
        {
            n.append(numbers[num % 10]);
        }
        if (num % 10 != 0 && len >= 0)
        {
            n.append(units[len]);
        }
        stack.push(n);
        num /= 10;
        len++;
        if (len > 12)
        {
            break;
        }
    }
    QString numStr = QString(preStr);
    while(stack.size() > 0)
    {
        numStr.append(stack.pop());
    }
    numStr.append((sufStr));
    return numStr;
}

QString NumberHelper::numToStrWithoutUnit(int num)
{
    return NumberHelper::numToStrWithoutUnit("", num, "");
}

QString NumberHelper::numToStrWithoutUnit(QString preStr, int num, QString sufStr)
{
    if (num <= 0)
        return QString("零");
    Qt::SplitBehavior behavior(Qt::SplitBehaviorFlags::SkipEmptyParts);
    QString nums = "零,一,二,三,四,五,六,七,八,九";
    QStringList numbers = nums.split(",", behavior, Qt::CaseSensitivity::CaseSensitive);
    QStack<QString> stack;
    while (num > 0)
    {
        stack.push(numbers[num % 10]);
        num /= 10;
    }
    QString numStr = QString(preStr);
    while(stack.size() > 0)
    {
        numStr.append(stack.pop());
    }
    numStr.append(sufStr);
    return numStr;
}
